<?php
/**
 ***** 扶摇而上、抱守本真 ******
 * @author Dana
 * @Date 2021/6/21
 */

function sum($carry,$item){
    echo 'carray: '.$carry.' item '.$item .PHP_EOL;
    $carry +=$item;
    echo 'sum'.PHP_EOL;
    return $carry;
}

$a = array(1,2,3,4,5);

//var_dump(array_reduce($a,'sum',10));

class MakeUp{
    public static function handle(Closure $next)
    {
        echo '化妆打扮，'.PHP_EOL;
        $next();
    }

}

class Skirt{
    public static function handle(Closure $next)
    {
        echo '穿上裙子，'.PHP_EOL;
        $next();
    }
}

$firstSlice = function(){
   echo '我要出去完了~，'.PHP_EOL;
};

$arr = [
    'MakeUp',
    'Skirt'
];

function getSlice(){
    return function($stack,$pipe){
       /* echo '<pre>';
        echo 'stack:';
        var_dump($stack);
        echo '<hr>';
        echo 'pipe:';
        var_dump($pipe);
        echo '</pre>';
        echo '<br><hr>';*/

        return function() use($stack,$pipe){
            return $pipe::handle($stack);
        };
    };
}

$go = array_reduce($arr, getSlice(), $firstSlice);

//$go();


$arr = array(
    array('min' => 1.5456, 'max' => 2.28548, 'volume' => 23.152),
    array('min' => 1.5457, 'max' => 2.28549, 'volume' => 23.152),
    array('min' => 1.5458, 'max' => 2.28550, 'volume' => 23.152),
    array('min' => 1.5459, 'max' => 2.28551, 'volume' => 23.152),
    array('min' => 1.5460, 'max' => 2.28552, 'volume' => 23.152),
);

$initial = array_shift($arr);


$t = array_reduce($arr, 'getS', $initial);

function getS($result,$item){
    //var_dump($result);
    //var_dump($item);
    $result['min'] = min($result['min'], $item['min']);
    $result['max'] = max($result['max'], $item['max']);
    $result['volume'] += $item['volume'];
   // var_dump('result');
   // var_dump($result);

    return $result;
}


function reduceToTable($html, $p){
    $html .="<TR><TD><a href=\"$p.html\">$p</a></TD><TR>\n";
    return $html;
}

$list = array('page1','page2','page3');

$tab = array_reduce($list, 'reduceToTable');

//echo "<table>".$tab."</table>";

$array = array(
    0 => array('id' => '100', 'name' => 'Henk', 'age' => '30'),
    1 => array('id' => '101', 'name' => 'Piet', 'age' => '33'),
    2 => array('id' => '102', 'name' => 'Wim', 'age' => '43'),
    3 => array('id' => '103', 'name' => 'Jaap', 'age' => '53'),
);


$arr = array_reduce($array,function($carry,$item){
    $arr = [
       'id'=>$item['id'],
       'value'=>$item['name'],
    ];

    $id = $item['id'];
    $carry[$id] = $arr;
    return $carry;
},array());

print_r(json_encode($arr));